package com.xigua.JucOfBeautiful.chapterOne.trreadCreateAndRun1_2;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author LiRongHua
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: 1-2 线程创建与运行
 * @date 2022/1/2021:55
 */
public class ThreadTest {

    public static class MyThread extends Thread{

        @Override
        public void run() {
            System.out.println("I am a child Thread");
        }
    }

    public static class RunableTask implements  Runnable{

        @Override
        public void run() {
            System.out.println("I am a child Thread");
        }
    }

    public static class CallerTask implements Callable{

        @Override
        public String call() throws Exception {
            return "hello";
        }
    }

    public static void main(String[] args) {
        //继承thread
        MyThread myThread = new MyThread();
        myThread.start();

        //实现runable接口
        RunableTask runableTask = new RunableTask();
        Thread thread1 = new Thread(runableTask);
        Thread thread2 = new Thread(runableTask);
        thread1.start();
        thread2.start();

        //继承callable接口
        FutureTask<String> futureTask = new FutureTask<>(new CallerTask());
        new Thread(futureTask).start();
        try {
            String result = futureTask.get();
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }




}
